package com.hd.pzhwlxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.Goods;
import com.hd.pzhwlxt.service.GoodsService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @create: 2022-09-22 11:16
 * @author: 怀念天空
 * @Description:
 */
@RestController
@RequestMapping("/goods")
public class CoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/get")
    public Response<Object> getAllGoods(@RequestParam(value = "pageNum",required = false) Integer pageNum, @RequestParam(value = "pageSize",required = false) Integer pageSize){
        if (pageNum == null || pageSize == null){
            List<Goods> result = goodsService.getAllGoodsAndPrice();
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }else {
            Page<Goods> result = goodsService.getAllGoodsAndPricePage(pageNum, pageSize);
            if(result!=null){
                return Response.createSuccessResponse("查询成功", result);
            }
        }
        return Response.createErrorResponse("查询失败");
    }

    @GetMapping("/get/{id}")
    public Response<Goods> getGoodsInfoById(@PathVariable Integer id){
        Goods result = goodsService.getGoodsById(id);
        if(result!=null){
            return Response.createSuccessResponse("查询成功", result);
        }
        return Response.createErrorResponse("查询失败");
    }

    @PostMapping("/add")
    public Response<String> addGoodsInfo(@RequestBody Goods goods){
        boolean save = goodsService.saveGoodsAndPrice(goods);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @DeleteMapping("/delmore/{ids}")
    public Response<String> deleteGoodsInfoByIds(@PathVariable Integer[] ids){
        boolean remove = goodsService.removeByIds(Arrays.asList(ids));
        if (remove){
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    @PutMapping("/update")
    public Response<String> updateGoodsInfo(@RequestBody Goods goods){
        boolean update = goodsService.updateGoods(goods);
        if (update){
            return Response.createSuccessResponse("修改成功");
        }
        return Response.createErrorResponse("修改失败");
    }
}
